package com.xperblueray.stepbystep.error;

import com.xperblueray.stepbystep.exception.BusinessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


// 定义统一的异常处理类，basePackages属性用于定义扫描哪些包，默认可不设置
@ControllerAdvice(basePackages = {"com.xperblueray.stepbystep"})
public class GlobalDefaultExceptionHandler {

    // 用来定义函数针对的异常类型，可以传入多个需要捕获的异常类
    @ExceptionHandler({BusinessException.class})
    // 如果返回的为json数据或其他对象，就添加该注解
    @ResponseBody
    public ErrorInfo defaultErrorHandler(HttpServletRequest request, Exception e) {
        ErrorInfo errorInfo = new ErrorInfo();
        errorInfo.setMessage(e.getMessage());
        errorInfo.setUrl(request.getRequestURL().toString());
        errorInfo.setCode(ErrorInfo.SUCCESS);

        return errorInfo;
    }
}
